package com.wuliu.controller;

import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.core.domain.entity.SysDept;
import com.wuliu.common.utils.KuaidiHelpUtil;
import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.dto.app.*;
import com.wuliu.system.domain.dto.applet.QryOrderFeeDto;
import com.wuliu.system.domain.vo.app.CountVo;
import com.wuliu.system.domain.vo.app.TspOrderVo;
import com.wuliu.system.service.*;
import com.wuliu.system.service.app.AppTspOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 运单Controller
 *
 * @author realue
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/app/order")
@Api(tags = {"运单控制器"})
public class TspOrderController {
    @Resource
    private AppTspOrderService appTspOrderService;
    @Resource
    private FileUploadService fileUploadService;
    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private ISysAppletUserService sysAppletUserService;
    @Resource
    private CommonService commonService;
    @Resource
    private ITspOrderPreService tspOrderPreService;

    @GetMapping("/getOrderInfo")
    @ApiOperation(value = "查询运单详情", notes = "包含路由和图片")
    @ApiImplicitParam(name = "orderNo", value = "运单号", dataType = "String", paramType = "query")
    public ApiResult<TspOrderVo> getOrderInfo(String orderNo) {
        return ApiResult.success(appTspOrderService.getOrderInfo(orderNo));
    }

    @PostMapping("/createOrder")
    @ApiOperation(value = "电子录单/传统录单", notes = "")
    public ApiResult<Boolean> createOrder(@RequestBody @Validated TspOrderDto dto) {
        return ApiResult.success(appTspOrderService.createOrder(dto));
    }

    @PostMapping("/editOrder")
    @ApiOperation(value = "编辑运单", notes = "")
    public ApiResult<Boolean> editOrder(@RequestBody @Validated TspOrderDto dto) {
        return ApiResult.success(appTspOrderService.editOrder(dto));
    }

    @PostMapping("/recOrderByImage")
    @ApiOperation(value = "图片收件", notes = "")
    public ApiResult<Boolean> recOrderByImage(@RequestBody @Validated RecOrderByImageDto dto) {
        return ApiResult.success(appTspOrderService.recOrderByImage(dto));
    }

    @PostMapping("/toPieceOrder")
    @ApiOperation(value = "到件扫描", notes = "")
    public ApiResult<Boolean> toPieceOrder(@RequestBody @Validated ToPieceOrderDto dto) {
        return ApiResult.success(appTspOrderService.toPieceOrder(dto));
    }

    @PostMapping("/sendOrder")
    @ApiOperation(value = "发件扫描", notes = "")
    public ApiResult<Boolean> sendOrder(@RequestBody @Validated SendOrderDto dto) {
        return ApiResult.success(appTspOrderService.sendOrder(dto));
    }

    @PostMapping("/recOrder")
    @ApiOperation(value = "收件/称重收件", notes = "")
    public ApiResult<Boolean> recOrder(@RequestBody @Validated RecOrderDto dto) {
        return ApiResult.success(appTspOrderService.recOrder(dto));
    }

    @PostMapping("/dispOrder")
    @ApiOperation(value = "派件扫描", notes = "")
    public ApiResult<Boolean> dispOrder(@RequestBody @Validated DispOrderDto dto) {
        return ApiResult.success(appTspOrderService.dispOrder(dto));
    }

    @PostMapping("/signOrder")
    @ApiOperation(value = "图片签收", notes = "")
    public ApiResult<Boolean> signOrder(@RequestBody @Validated SignOrderDto dto) {
        return ApiResult.success(appTspOrderService.signOrder(dto));
    }

    @GetMapping("/checkOrderNo")
    @ApiOperation(value = "校验运单号", notes = "")
    public ApiResult<Boolean> checkOrderNo(String orderNo) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        return ApiResult.success(commonService.checkOrderNo(orderNo, user.getDeptNo(), user.getSysAccount()));
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件", notes = "请求格式:multipart/form-data")
    @ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query")
    public ApiResult<String> uploadFile(@RequestPart("file") MultipartFile file) {
        return ApiResult.success(fileUploadService.uploadFile(file, ""));
    }

    @GetMapping("/resolveAddress")
    @ApiOperation(value = "地址解析", notes = "地址")
    public ApiResult<Map<String, Object>> resolveAddress(String address) throws Exception {
        if (StringUtils.isEmpty(address)) {
            return ApiResult.error("地址不能为空");
        }
        return ApiResult.success(KuaidiHelpUtil.geocodingAddress(address));
    }

    @GetMapping("/getSysDepts")
    @ApiOperation(value = "查询站点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptNo", value = "站点编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptName", value = "站点名称", dataType = "integer", paramType = "query")
    })
    public ApiResult<List<SysDept>> getSysDepts(String deptNo, String deptName) {
        return ApiResult.success(sysDeptService.getSysDepts(deptNo, deptName));
    }

    @GetMapping("/getAppUsers")
    @ApiOperation(value = "查询业务员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userNo", value = "业务员编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "业务员名称", dataType = "integer", paramType = "query")
    })
    public ApiResult<List<SysAppletUser>> getAppUsers(String userNo, String userName) {
        return ApiResult.success(sysAppletUserService.getAppUsers(userNo, userName));
    }

    @GetMapping("/orderCount")
    @ApiOperation(value = "app首页统计")
    public ApiResult<CountVo> orderCount() {
        return ApiResult.success(appTspOrderService.orderCount());
    }

    @GetMapping("/queryFee")
    @ApiOperation(value = "运费查询", notes = "")
    public ApiResult<BigDecimal> calcFee(@Validated QryOrderFeeDto dto) {
        return ApiResult.success(tspOrderPreService.qryFee(dto));
    }
}
